package com.fenxiangtech.mall.controller;

import com.alibaba.fastjson.JSON;
import com.fenxiangtech.mall.base.BaseResponse;
import com.fenxiangtech.mall.base.PageData;
import com.fenxiangtech.mall.manager.OrderProductManager;
import com.fenxiangtech.mall.model.OrderProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lixiang
 * @CreateTime 2018/10/27
 **/
@Controller
public class OrderProductController extends AbstractBaseController {
    @Autowired
    private OrderProductManager orderProductManager;

    @GetMapping("/orderProduct/query")
    @ResponseBody
    public BaseResponse<PageData<OrderProduct>> queryOrderProduct(OrderProduct orderProduct, Integer pageIndex , Integer pageSize){

        List<OrderProduct> orderProducts = orderProductManager.queryOrderProduct(orderProduct, pageIndex, pageSize);
        Long totalCount = orderProductManager.countOrderProduct(orderProduct);

        pageIndex = pageIndex == null?1:pageIndex;
        pageSize = pageSize == null?20:pageSize;
        return  BaseResponse.assemblePageResponse(orderProducts,totalCount,pageIndex,pageSize);
    }

    @PostMapping("/orderProduct/save")
    @ResponseBody
    public BaseResponse<String> saveItem(OrderProduct orderProduct){
        BaseResponse<String> baseResponse = new BaseResponse<>();
        orderProductManager.saveOrderProduct(orderProduct);
        baseResponse.setData("OK");
        return  baseResponse;
    }

    @GetMapping("/orderProduct/list")
    @ResponseBody
    public String listItem(OrderProduct orderProduct, Integer pageNumber , Integer pageSize){

        Map<String,Object> map  = new HashMap<>();

        List<OrderProduct> orderProducts = orderProductManager.queryOrderProduct(orderProduct, pageNumber, pageSize);
        Long totalCount = orderProductManager.countOrderProduct(orderProduct);
        map.put("total",totalCount);
        map.put("rows",orderProducts);
        return JSON.toJSONString(map);
    }
    
    @GetMapping("/orderProduct/get")
    @ResponseBody
    public OrderProduct listItem(OrderProduct orderProduct){
    
        Map<String,Object> map  = new HashMap<>();
        List<OrderProduct> orderProducts = orderProductManager.queryOrderProduct(orderProduct, 1, 1);
        if(orderProducts!=null && orderProducts.size()>0){
            return orderProducts.get(0);
        }else{
            return new OrderProduct();
        }
    }

    @GetMapping("/orderProduct/delete")
    @ResponseBody
    public String delete(Long id){
        orderProductManager.deleteOrderProduct(id);
        return "OK";
    }

}
